<template>
  <div>
    <!-- 顶部导航 -->
    <van-nav-bar class="page-nav-bar" title="登录">
      <van-icon @click="$router.go(-1)" class="left-icon" slot="left" name="cross"></van-icon>
    </van-nav-bar>
    <!-- 表单 -->
    <van-form @submit="onSubmit" ref="loginForm">
      <van-field v-model="mobile" name="mobile" placeholder="请输入手机号" :rules="userFormRules.mobile" type="number"
        maxlength="11">
        <i slot="left-icon" class="toutiao toutiao-shouji"></i>
      </van-field>
      <van-field v-model="code" name="code" placeholder="请输入验证码" :rules="userFormRules.code">
        <i slot="left-icon" class="toutiao toutiao-yanzhengma"></i>
        <template #button>
          <van-count-down format="ss s" @finish="isShowCountDown = false" v-if="isShowCountDown" :time="60 *1000" />
          <van-button v-else class="send-sms-btn" size="small" type="primary" native-type="button" @click="clickCode">
            发送验证码</van-button>
        </template>
      </van-field>
      <div style="margin:16px;" login-btn-wrap>
        <van-button round block type="info" native-type="submit">提交</van-button>
      </div>
    </van-form>
  </div>
</template>

<script>
import { login, sendCode } from '@/api/user.js'
export default {
  data () {
    return {
      mobile: '',
      code: '',
      userFormRules: {
        mobile: [
          { required: true, message: '请输入手机号' },
          { pattern: /^1[3|5|7|8]\d{9}$/, message: '手机号码格式不正确' }
        ],
        code: [
          { required: true, message: '请输入验证码' },
          { pattern: /^\d{6}$/, message: '验证码格式为6位数字' }
        ]
      },
      isShowCountDown: false
    }
  },
  methods: {
    async onSubmit () {
      this.$toast.loading({
        message: '加载中...',
        duration: 0, // 持续提示
        forbidClick: true
      })
      try {
        const res = await login({
          mobile: this.mobile,
          code: this.code
        })
        // 将数据存储到vuex
        this.$store.commit('initUser', res.data.data)
        // 成功提示
        this.$toast.success('登录成功')
        // 跳转
        this.$router.go(-1)
      } catch (err) {
        if (err.response && err.response.status === 400) {
          return this.$toast.fail('账号或用户名错误')
        }
        this.$toast.fail('登录失败')
      }
    },
    // 发送验证码
    async clickCode () {
      try {
        await this.$refs.loginForm.validate('mobile')
      } catch (err) {
        return
      }

      // 校验成功的操作
      this.$toast.loading({
        message: '发送中...',
        duration: 0, // 持续提示
        forbidClick: true
      })

      // 发送验证码的请求
      try {
        await sendCode(this.mobile)
        this.isShowCountDown = true
        this.$toast.success('发送成功')
      } catch (err) {
        if (err.response && err.response.status === 429) {
          return this.$toast.fail('发送频繁')
        }
        this.$toast.fail('系统异常')
      }
    }
  },
  computed: {},
  watch: {},
  filters: {},
  components: {},
  created () { },
  mounted () { }
}
</script>

<style scoped lang='less'>
.toutiao {
  font-size: 37px;
}
.send-sms-btn {
  width: 152px;
  height: 46px;
  line-height: 46px;
  background-color: #ededed;
  font-size: 22px;
  color: #666;
  border: 0;
  border-radius: 20px;
}
.login-btn-wrap {
  padding: 53px 33px;
  .login-btn {
    background-color: #6db4fb;
    border: none;
  }
}
</style>
